/** 
 * PreviewDialog.java
 * @author Lex
 * 2007-9-1 ����12:20:00
 */
package org.lex.notepad.ui.filechooser;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class PreviewDialog extends JDialog {
	private JPanel pane = null;
	private JFileChooser fileChooser = null;
	private JPanel controlPane = null;
	private JPanel buttonsPane = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JButton detailsButton = null;
	// ===================================
	private Component detailsComponent = null;

	public static void main(String[] args) {
		new PreviewDialog(null).setVisible(true);
	}

	public PreviewDialog(Window owner) {
		super(owner);
		initialize();
	}

	private void initialize() {
		this.setSize(500, 400);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getPane());
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(this.getFileChooser(), BorderLayout.CENTER);
			pane.add(getControlPane(), BorderLayout.SOUTH);
		}
		return pane;
	}

	private JFileChooser getFileChooser() {
		if (null == fileChooser) {
			fileChooser = new JFileChooser();
			this.fileChooser.setControlButtonsAreShown(false);
		}
		return this.fileChooser;
	}

	private JPanel getControlPane() {
		if (controlPane == null) {
			controlPane = new JPanel();
			controlPane.setLayout(new BorderLayout());
			controlPane.add(getButtonsPane(), BorderLayout.NORTH);
		}
		return controlPane;
	}

	private JPanel getButtonsPane() {
		if (buttonsPane == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.RIGHT);
			buttonsPane = new JPanel();
			buttonsPane.setLayout(flowLayout);
			buttonsPane.add(getOkButton(), null);
			buttonsPane.add(getCancelButton(), null);
			buttonsPane.add(getDetailsButton(), null);
		}
		return buttonsPane;
	}

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
		}
		return okButton;
	}

	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
		}
		return cancelButton;
	}

	private JButton getDetailsButton() {
		if (detailsButton == null) {
			detailsButton = new JButton();
			detailsButton.setText("Details >>");
			detailsButton.addActionListener(new ActionListener() {
				private boolean shown = false;
				private Dimension size = getSize();

				public void actionPerformed(ActionEvent e) {
					if (false == shown) {
						// ��ʾ
						getControlPane().add(getDetailsComponent(), BorderLayout.CENTER);
						detailsButton.setText("<< Details");
						getSize(size);
						setSize(size.width, size.height + 100);
					} else {
						// ����
						getControlPane().remove(getDetailsComponent());
						detailsButton.setText("Details >>");
						setSize(getWidth(), size.height);
					}
					shown = !shown;
					validate();
				}
			});
		}
		return detailsButton;
	}

	private Component getDetailsComponent() {
		if (null == this.detailsComponent) {
			JPanel pane = new JPanel(new BorderLayout());
			JTextArea t = new JTextArea(5, 20);
			t.setText(this.getText());
			pane.add(t, BorderLayout.CENTER);
			this.detailsComponent = pane;
		}
		return this.detailsComponent;
	}

	private String getText() {
		StringBuilder b = new StringBuilder();
		Random r = new Random();
		for (int i = 0; i < 100; i++)
			b.append(Long.toString(r.nextLong(), 36)).append(' ');
		return b.toString();
	}
}
